package com.wuercloud.mq.message.consumer.init;

import com.wuercloud.mq.config.RabbitMqInInitializer;
import com.wuercloud.mq.message.converter.FastJsonMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by JiangWL on 2017/4/5.
 */
@Component
public class ConsumerInitializer {
    private static Logger logger = LoggerFactory.getLogger(ConsumerInitializer.class);
    private Map<String,Exchange> exchangeMap = new ConcurrentHashMap<>();
    private Map<String,Queue> queueMap = new ConcurrentHashMap<>();

    @Autowired
    private RestTemplate restClientTemplate;
    @Autowired
    private ConnectionFactory rabbitConnectionFactory;
    @Autowired
    private SimpleMessageListenerContainer simpleMessageListenerContainer;
    @Autowired
    private FastJsonMessageConverter fastJsonMessageConverter;
    @Autowired
    private RabbitMqInInitializer rabbitMqInInitializer;

    /**
     * 注入Bean时初始化rabbitmq配置。构建交换器、队列及绑定等
     */
    @PostConstruct
    void init(){
        System.out.println("初始消费者队列配置内容。。。");
        final List<String> bindKeyStrList = rabbitMqInInitializer.getMqConfigList();
        initRabbitDirectExchangeMap(bindKeyStrList);
        initRabbitQueueMap(bindKeyStrList);
    }

    private void initRabbitDirectExchangeMap(final List<String> bindKeyStrList){
        bindKeyStrList.stream().forEach(bindKey ->setExchange(null,bindKey));
    }
    private void initRabbitQueueMap(final List<String> bindKeyStrList){
        bindKeyStrList.stream().forEach(bindKey ->setQueue(null,bindKey));
    }
    private void setQueue(final Queue queue,final String bindKey){
        if (queue!=null){
            this.queueMap.put(queue.getName(),queue);
            this.simpleMessageListenerContainer.addQueueNames(queue.getName());
            return;
        }
        if (!this.queueMap.containsKey(bindKey)){
            //初始的下面内容未读取接口获取，应做成接口获取
            Queue que = new Queue(bindKey,true,false,false);
            this.queueMap.put(bindKey,que);
            //注册队列至监听器
            this.simpleMessageListenerContainer.addQueueNames(bindKey);
        }
    }
    private void setExchange(final Exchange ex,final String bindKey){
        if (ex!=null){
            this.exchangeMap.put(ex.getName(),ex);
            return;
        }
        if (!this.exchangeMap.containsKey(bindKey)) {
            final String newKey = "520WAWA-".concat(bindKey.substring(0,bindKey.lastIndexOf("-")));
            //初始的下面内容未读取接口获取，应做成接口获取
            DirectExchange exchange = new DirectExchange(newKey,true,false);
            this.exchangeMap.put(bindKey,exchange);
        }
    }

    public void insertExchange(Exchange exchange){
        setExchange(exchange,null);
    }
    public void insertQueue(Queue que){
        setQueue(que,null);
        rabbitMqInInitializer.setTempalte("520WAWA-".concat(que.getName().substring(0,que.getName().lastIndexOf("-"))));
    }

    public Map<String, Exchange> getExchangeMap() {
        if (this.exchangeMap==null || this.exchangeMap.isEmpty()){
            initRabbitDirectExchangeMap(rabbitMqInInitializer.getMqConfigList());
        }
        return exchangeMap;
    }

    public Map<String, Queue> getQueueMap() {
        if (this.queueMap==null || this.queueMap.isEmpty()){
            initRabbitQueueMap(rabbitMqInInitializer.getMqConfigList());
        }
        return queueMap;
    }
}
